using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Scheduler;

public partial class UserLogin : System.Web.UI.Page
{
    private String userID = "";
    private String userType = "";
    /*
     * this method loads the current page
     */
    protected void Page_Load(object sender, EventArgs e)
    {
        
        Database DB = new Database();
        DB.OpenDB();
        if (Context.Items["User_Type"] != null)
        {
            /* receive the user type from the context */
            userType = Context.Items["User_Type"].ToString();
            dlstUserType.SelectedValue = userType;
        }
        else
        {
            userType = dlstUserType.SelectedValue;
        }

        /* enable/disable the new customer option */
        if (userType == "Customer")
        {
            chkNew.Visible = true;
        }
        else
        {
            chkNew.Visible = false;
        }

        DB.CloseDB();
    }

    public String RMT_ID
    {
        get { return userID; }
    }

    protected void dlstUserType_SelectedIndexChanged(object sender, EventArgs e)
    {
        userType = dlstUserType.SelectedValue;
    }
    /*
     * this method handles the login click event
     */
    protected void btnLogin_Click(object sender, EventArgs e)
    {
        if (txtUserID.Text == "")
        {
            lblLoginFailed.Visible = true;
            return;
        }
        else
        {
            /* create a new user from the given information */
            User user = new User(txtUserID.Text, txtPassword.Text, dlstUserType.SelectedIndex);
            Database db = new Database();
            bool failed = false;
            db.OpenDB();

            /* retrieves the list of all users from Database */
            LinkedList<User> userList = db.readAllUsers(dlstUserType.SelectedIndex);

            /* is this a new customer? */
            if ( chkNew.Checked )
            {
                /* make sure the user does not exist */
                if (userList.Contains(user))
                {
                    failed = true;
                }
                else
                {
                    /* update the user list & customer info in the database */
                    userList.AddLast(user);
                    Customer cust = new Customer(txtUserID.Text, txtUserID.Text, 0);
                    user.writeToDatabase(db);
                    cust.writeToDatabase(db);
                }
            }
            if ( !failed && userList.Contains(user) && user.login(db) )
            {
                /* redirect the user to the profile page */
                Context.Items["User_ID"] = txtUserID.Text;
                Context.Items["User_Type"] = dlstUserType.SelectedIndex;
                Server.Transfer("UserProfile.aspx");
            }
            else
            {
                /* display the log in failed message */
                lblLoginFailed.Visible = true;
            }

            db.CloseDB();
        }
    }
}
